package com.tbdgroup.babycare;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.tbdgroup.babycare.chart.ChartActivity;
import com.tbdgroup.babycare.db.DBManager;
import com.tbdgroup.babycare.db.Event;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider
 */
public class FunctionPanelActivity extends Activity {
    
    private DBManager mgr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_function_panel);

        mgr = new DBManager(this);
    }
    
    public void eat(View view) {
    	handleClickForStartEnd(Event.E_TYPE.EAT);
    }
    
    public void sleep(View view) {
    	handleClickForStartEnd(Event.E_TYPE.SLEEP);
    }
    
    public void pee(View view) {
    	handleClickForOnce(Event.E_TYPE.PEE);
    }
    
    public void shit(View view) {
    	handleClickForOnce(Event.E_TYPE.SHIT);
    }

	private void handleClickForStartEnd(Event.E_TYPE type) {
		Event event = new Event();
    	event.type = type.name();
    	int id = mgr.get(type);
    	if(id < 0) {
    		event.start = new Date().getTime() + "";
    		event.end = "";
    		mgr.add(event);
    	} else {
    		mgr.updateEnd(id);
    	}
	}
	
	private void handleClickForOnce(Event.E_TYPE type) {
		Event event = new Event();
    	event.type = type.name();
    	event.start = new Date().getTime() + "";
    	event.end = "";
    	mgr.add(event);
	}
	
	public void goChart(View view) {
		Intent intent = new Intent(this, ChartActivity.class);
		startActivity(intent);
	}
	
    @Override  
    protected void onDestroy() {  
        super.onDestroy();
        mgr.closeDB();  
    }  
}
